package com.gzist.blog_system.controller;

import com.gzist.blog_system.domain.Comment;
import com.gzist.blog_system.domain.result.PageBean;
import com.gzist.blog_system.domain.result.Result;
import com.gzist.blog_system.domain.vo.CommentPageVO;
import com.gzist.blog_system.service.CommentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/comment")
@Api(tags = "文章评论模块")
public class CommentController {

    @Resource
    private CommentService commentService;

    /*
     * @description:
     *  分页展示某个文章的评论
     * @author: 黄泽建
     **/
    @GetMapping("/list")
    @ApiOperation("分页展示文章评论")
    public Result selectCommentWithPage(@RequestBody @ApiParam("当前文章id")CommentPageVO commentPageVO) {
        PageBean<Comment> pb = commentService.queryPage(commentPageVO);
        log.info("操作到的数据====>{}", pb);
        return Result.success(pb);
    }

    /*
     * @description:
     *  后台查询最新的几条评论
     * @author: 黄泽建
     **/
    @GetMapping("/getNewComment")
    @ApiOperation("后台查询最新评论")
    public Result getNewComment() {
        List<Comment> list = commentService.getNewComment();
        return Result.success(list);
    }

    /*
     * @description:
     *  发布评论
     * @author: 黄泽建
     **/
    @PostMapping("/insert")
    @ApiOperation("发布评论")
    public Result pushComment(@RequestBody @ApiParam("评论数据") Comment comment) {
        boolean result =  commentService.pushComment(comment);
        if (!result) return Result.error("发布失败");
        return Result.success();
    }

    /*
     * @description:
     *  站点服务统计，统计评论数量
     * @author: 黄泽建
     **/
    @GetMapping("/countComment")
    @ApiOperation("统计评论数量")
    public Result countComment() {
        Long result =  commentService.countComment();
        return Result.success(result);
    }

    /*
     * @description:
     *  通过文章id删除评论信息
     * @author: 黄泽建
     **/
    @PostMapping("/delete")
    @ApiOperation("删除评论")
    public Result deleteComment(@RequestParam @ApiParam("评论数据") Long id) {
        boolean result =  commentService.deleteComment(id);
        if (!result) return Result.error("删除失败");
        return Result.success();
    }
}
